import redis
import time



def promote_redis_slave_to_master(host):
    r = redis.Redis(host=host, port=6379)

    # Execute the REPLICAOF NO ONE command to promote the slave to a master
    r.execute_command('SLAVEOF NO ONE')

    # Optionally, you can wait for the promotion to complete and verify the new role
    # Note that this may not be necessary in all cases, especially if you trust the Redis server to handle the transition promptly
    while True:
        role_info = r.role()
        if role_info[0] == b'master':
            print(f"{host}: The Redis instance is now a master.")
            break
        else:
            print("Promotion in progress...")
            time.sleep(1)  # Wait for a short period before checking again
            

            
def set_db_count(host, count):
    r = redis.Redis(host=host, port=6379)
    r.execute_command(f"hset elect db_change_count {count}")
    

def get_role(host):
    r = redis.Redis(host=host, port=6379, socket_connect_timeout=1)
    return r.role()
    
    
# promote_redis_slave_to_master()
# set_db_count(7)